package cn.itcast.bassm.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.itcast.bassm.mapper.PushMapper;
import cn.itcast.bassm.pojo.Push;
import cn.itcast.bassm.pojo.Returnxx;
import cn.itcast.bassm.pojo.Unitdoor;
import cn.itcast.bassm.pojo.Uploadfile;
import cn.itcast.bassm.pojo.User;
import cn.itcast.bassm.pojo.UserInformation;
import cn.itcast.bassm.pojo.Userformation;
import cn.itcast.bassm.pojo.Visitors;
import cn.itcast.bassm.service.PushService;
import cn.itcast.bassm.service.UserService;
import cn.itcast.bassm.util.readTxT;

import com.alibaba.fastjson.JSONObject;

@RequestMapping("/Visitor")
@Controller
public class VisitorController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private PushService pushService;
	
	@Autowired
	private PushMapper pushMapper;
	
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ResponseEntity<User> queryUserById(@PathVariable("id")int id){
		
		try {
			User user= userService.queryUserById(id);
			return ResponseEntity.ok(user); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);	
	}

	
	/**
	 * 告警推送
	 * @param Pushmessage
	 */
	@RequestMapping(value="/savemsg",method=RequestMethod.POST)
	public void savemsg(@RequestBody String Pushmessage){
		JSONObject jsonObject = JSONObject.parseObject(Pushmessage);
		String scenePath = jsonObject.getString("scenePath");
		long datelong = jsonObject.getLongValue("timestamp");
		//pushService.savePush(scenePath, datelong);
	}
	
	/**
	 * 登陆
	 */
	@RequestMapping(value="/addlogin",method=RequestMethod.POST)
	@ResponseBody
	public Userformation addlogin(String username,String password){
		Userformation userformation = new Userformation();
		userformation= userService.queryUserByPowrd(username, password);
		return userformation;
	}
	
	/**
	 * 删除用户
	 * @throws Throwable 
	 */
	@RequestMapping(value="/deleteuserone",method=RequestMethod.POST)
	@ResponseBody
	public Returnxx deleteuserone(String yid,String detailed) throws Throwable{
		Returnxx rb = new Returnxx();
		String msg = null;
		
		Date data3=new Date();
        SimpleDateFormat dateFormat3=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String updatetime = dateFormat3.format(data3);
        
        Date data=new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMdd");
        String stateupdatetime = dateFormat.format(data);
        
		String yindate="0";
		String yupdatetime=updatetime;
		String state = stateupdatetime;
		String visitorstype = null;
		
		try {
			pushMapper.delecevisitorsone(yid,yupdatetime,visitorstype,yindate);
		} catch (Exception e) {
			e.printStackTrace();
			msg="2";
			rb.setDetailed(detailed);
			rb.setMsg(msg);
			return rb;
		}
		msg="1";
		rb.setDetailed(detailed);
		rb.setMsg(msg);
		return rb;
	}
	
	/**
	 * 人脸检测
	 */
	/*//addWhitePersion
	@RequestMapping(value="/addWhitePersion",method=RequestMethod.POST)
	@ResponseBody
	public String addWhitePersion(String filepath, String name, String phone,String detailed,String sex,String sfzid,int state){
		boolean msg=userService.addWhitePersion(filepath,name,phone,detailed,sex,sfzid,state);
		if(!msg){
			String usermsg="0";
			return usermsg;
		}else {
			String usermsg="1";
			return usermsg;
		}
	}*/
	/**
	 * 保存用户信息
	 */
	
	@RequestMapping(value="/UserInformation",method=RequestMethod.POST)
	@ResponseBody
	public String UserInformation(MultipartFile file, String name, String phone,String sfzid,String detailed,String sex){
		String phong=phone;
		int state=1;
		String msg = userService.addUserInformation(file, name, sex, phong,sfzid, detailed,state);
		return msg;
	}
	
	
	/**
	 * 用户信息列表展示
	 */
	@RequestMapping(value="/ListUserInformation",method=RequestMethod.GET)
	public String ListUserInformation(ModelMap map){	
		List<UserInformation> userinformation=pushService.finPushUser();
		map.addAttribute("userinformation",userinformation);
		return "showAll";
	}

	/**
	 * 选择图片人脸检测
	 */
	@RequestMapping(value="/UserInformationFist",method=RequestMethod.POST)
	@ResponseBody
	public String UserInformationFist(String filepath){;
	String msg=userService.addFacePathFist(filepath);
		return msg;
	}
	
	
	/**
	 * 允许加入白名单
	 * @throws Throwable 
	 */
	@RequestMapping(value="/addUserInformationFist",method=RequestMethod.POST)
	@ResponseBody
	public Returnxx addUserInformationFist(String id,String num) throws Throwable{
		Returnxx rb = new Returnxx();
		rb= pushService.upfkuseronetwo(id,num);
		return rb;
	}
	
	
	/**
	 * 分页查询用户数据展示
	 */
	@RequestMapping(value="/showMyProduct",method=RequestMethod.GET)
	public String showMyProduct(String pageNow, Model model){	
		//this.userService.showProductsByPage(pageNow,model);
		return "showAll";
	}
	
	/**
	 * 禁止用户加入白名单
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/DeleUserInformationFist",method=RequestMethod.GET)
	@ResponseBody
	public Returnxx DeleUserInformationFist(String id) throws UnsupportedEncodingException{
		
		Returnxx rb = new Returnxx();
		rb =pushService.delefkuseronetwo(id);
		return rb;
	}
	
	
	/**
	 * 查询用户图片返回注册页面
	 */
	@RequestMapping(value="/showMyUserImg",method=RequestMethod.GET)
	public String showMyUserImg( Model model){	
		List<Push>push=pushService.showFinUserImg();
		model.addAttribute("push", push);
		return "add2";
	}
	
	/**
	 * 推送头像注册页面保存用户信息
	 */
	@RequestMapping(value="/UserImgFistToto",method=RequestMethod.POST)
	@ResponseBody
	public String UserImgFistTo(String urlimgfisto, String name, String phone,String sfzid,String detailed,String sex){
		String phong=phone;
		int state=1;
		String str =urlimgfisto;
		String txtPath ="/usr/local/tomcat/tomcat8/webapps/configSEX.txt";
		String TimeSex=readTxT.readTxtLine(txtPath, 2);
		String strimg=str.replaceAll(TimeSex, "");
		String msg = userService.addUserImgToFist(strimg, name, sex, phong, sfzid, detailed, state);
		return msg;
	}
	
	/**
	 * 用户信息列表删除
	 */
	@RequestMapping(value="/DeleUserImg",method=RequestMethod.POST)
	@ResponseBody
	public Returnxx DeleUserImg(String yid,String yaddress){
		Returnxx rb = new Returnxx();
		rb = userService.deleimg(yid,yaddress);
		return rb;
	}
	
	/**
	 * 选择图片检测人脸
	 */
	@RequestMapping(value="/UseImgUploadFin",method=RequestMethod.POST)
	@ResponseBody
	public String UserImgUpload(MultipartFile file){
		String msg =userService.UserimgUploadFin(file);
		return msg;
	}
	
	/**
	 * 展示页面超级管理员
	 */
	@RequestMapping(value="/visitorsuser1",method=RequestMethod.GET)
	public String visitorsuser1( Model model){
		return "NewFile";
	}
	/**
	 * 展示页面业主
	 */
	@RequestMapping(value="/visitorsuser2",method=RequestMethod.GET)
	public String visitorsuser2(String pageNow, String Celladdress,Model model){
		String str=Celladdress;	 
		byte[] bytes;
		byte[] bytes2;
		try {
			bytes = str.getBytes("ISO-8859-1");
			String name=new String(bytes,"utf-8");

			this.userService.showProductsByPage(pageNow,name,model);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		
		return "showAll";
	}
	/**
	 * 展示页面访客
	 */
	@RequestMapping(value="/visitorsuser3",method=RequestMethod.GET)
	public String visitorsuser3( String pageNow, String Celladdress,Model model){
		String str=Celladdress;	 
		byte[] bytes;
		try {
			bytes = str.getBytes("ISO-8859-1");
			String name=new String(bytes,"utf-8");
			this.pushService.showBvisitorProductsByPage(pageNow, name, model);
			} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
		return "2";
	}
	
	/**
	 * 根据用户名或者手机好进行查询
	 */
	@RequestMapping(value="/selectFinPhoneorUserName",method=RequestMethod.GET)
	public String selectFinPhoneorUserName(String phonename,String nameonefin,String yaddressall,String pageNow,Model model){
		String str=nameonefin;
		String str2=yaddressall;
		byte[] bytes;
		byte[] bytes2;
		try {
			bytes = str.getBytes("ISO-8859-1");
			bytes2 = str2.getBytes("ISO-8859-1");
			String name=new String(bytes,"utf-8");
			String name2=new String(bytes2,"utf-8");
			String nameonefinname =name;
			String yaddressallall=name2;
			this.pushService.selectFinPhoneorUserNamefin(phonename, nameonefinname,yaddressallall, pageNow, model);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		return "xiu4";
		}
	
	/**
	 * 根据用户的id 小区地址 进行查询
	 */
	@RequestMapping(value="/selectFinPhoneorUserNameLou",method=RequestMethod.GET)
	public String selectFinPhoneorUserNameLou(String yid,String yaddress, Model model){	
		String str=yaddress;
		byte[] bytes;
		try {
			bytes = str.getBytes("ISO-8859-1");
			String name=new String(bytes,"utf-8");
			String yaddressallall=name;
		List<Visitors>products = new ArrayList<Visitors>();
		List<Unitdoor> unitdoor = new ArrayList<Unitdoor>();
			products = pushService.selectloufin(yid);
			unitdoor = pushService.selectloufinuser(yaddressallall);
			model.addAttribute("products", products);  
			model.addAttribute("unitdoor", unitdoor);  
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "xiugai2";
	}
	
	/**
	 * 用户信息修改保存
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/upUseroneFinset",method=RequestMethod.POST)
	@ResponseBody
	public Returnxx upUseroneFinset(String yid,String yunitdoor,String yaddress,String yname) throws UnsupportedEncodingException{
		Returnxx rb = new Returnxx();
		try {
		    int id = Integer.parseInt(yid);
		    
			rb=pushService.UpselcetUserXxFin(id, yunitdoor, yaddress,yname);
			
		} catch (NumberFormatException e) {
		    e.printStackTrace();
		}
			
		return rb;	
	}
	
	/**手机搜索页面跳转
	 * 
	 */
	/**
	 * 根据用户的id 小区地址 进行查询
	 */
	@RequestMapping(value="/selectponediao",method=RequestMethod.GET)
	public String selectponediao(String yaddress, Model model){	
		String str=yaddress;
		byte[] bytes;
		try {
			bytes = str.getBytes("ISO-8859-1");
			String name=new String(bytes,"utf-8");
			String yaddressallall=name;
			model.addAttribute("yaddress", yaddressallall); 
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		return "xiu3";
	}
	
	/**
	 * baihei展示页面访客
	 */
	@RequestMapping(value="/visitorsuser4",method=RequestMethod.GET)
	public String visitorsuser4( String pageNow, String Celladdress,String index,Model model){
		String str=Celladdress;	 
		byte[] bytes;
		try {
			bytes = str.getBytes("ISO-8859-1");
			String name=new String(bytes,"utf-8");
			this.pushService.showbaiheiuserxinxi(pageNow, name, index,model);
			} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
		return "xiu5";
	}
	
}
